﻿@{
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    List<WeChatParking.Entity.Enterprise> list = ViewBag.EnterpriseList;
}
<div class="">
    <div class="ibox-content">
        <div class="jqGrid_wrapper">
            <div class="col-sm-12 jqGrid-toolbar">
                <form class="form-inline search-form" id="search-form">
                    <label for="search_card_no">停车场</label>
                    <select class="form-control  sm" name="CCEnterpriseID" style="width: 120px;">
                        @foreach (var item in list)
                        {
                            <option value="@item.ID">@item.Enterprise_Name</option>
                        }
                    </select>
                    <div class="form-group">
                        <label for="search_order_id">订单号</label>
                        <input type="text" class="form-control" id="search_order_id" name="order_id">
                    </div>
                    <div class="form-group">
                        <label for="search_user_name">姓名</label>
                        <input type="text" class="form-control" id="search_user_name" style="width: 90px;" name="Wxuser_name">
                    </div>
                    <div class="form-group">
                        <label for="search_card_no">车牌号</label>
                        <input type="text" class="form-control" id="search_card_no" style="width: 90px;" name="car_no">
                    </div>
                    <div class="form-group">
                        <label for="search_pay_status">支付状态</label>
                        <select class="form-control  sm" id="search_pay_status" name="pay_status" onchange="gradeChange()">
                            <option value="0">全部</option>
                            <option value="1">未支付</option>
                            <option value="3">已支付</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="search_paytime_begin">创建时间</label>
                    </div>
                    <div class="form-group">
                        <div>
                            <input type="text" class="form-control laydate-icon layer-date" id="search_paytime_begin" name="paytime_begin" value="@DateTime.Today.AddDays(-6).ToString("yyyy-MM-dd")">
                            <input type="text" class="form-control laydate-icon layer-date" id="search_paytime_end" name="paytime_end" value=" @DateTime.Today.ToString("yyyy-MM-dd")">
                        </div>

                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="glyphicon glyphicon-search"></i>查询
                    </button>
                </form>
                <div class="btn-group " role="group">
                   @* <button class="btn btn-danger" id="btn-del" hidden="hidden">
                        <i class="glyphicon glyphicon-remove-circle"></i>退款
                    </button>*@
                    <button class="btn btn-primary" id="btn-export">
                        <i class="glyphicon glyphicon-download-alt"></i>导出
                    </button>
                </div>
            </div>
            <table id="grid"></table>
            <div id="grid_pager"></div>
        </div>
    </div>
</div>
<div class="modal fade" id="form-modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">角色信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" method="post" action="/Admin/RetryOrders">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">登陆密码：</label>
                            <div class="col-sm-9">
                                <input type="password" name="UserPwd" id="UserPwd" class="form-control" placeholder="请输入登陆密码" required maxlength="50">
                                <input type="hidden" id="order_id" name="order_id" class="form-control">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭操作</button>
                <button type="button" class="btn btn-primary" id="btn-save">提交退款</button>
            </div>
        </div>
    </div>
</div>
@section BodyFooter{
    <script>
        $(document).ready(function () {

            $("#grid").jqGrid({
                url: '/Admin/OrderQuery',
                multiselect: false,
                colNames: ["订单号", "姓名", "车牌号", "支付金额", "支付状态", "类型", "物业", "停车场", "支付时间", "创建时间"],
                colModel: [{
                    name: "order_id",
                    index: "order_id",
                    key: true,
                },
                {
                    name: "Wxuser_name",
                    index: "Wxuser_name",
                    width: 80
                },
                {
                    name: "car_no",
                    index: "car_no",
                    width: 80
                },
                {
                    name: "total_fee",
                    index: "total_fee",
                    align: 'right',
                    width: 70,
                    formatter: "currency"
                },
                {
                    name: "pay_status",
                    index: "pay_status",
                    formatter: formatPayStatus,
                    width: 70
                }
                ,
                {
                    name: "pay_type",
                    index: "pay_type",
                    formatter: formatPayType,
                    width: 70
                },
                 {
                     name: "Enterprise_Name",
                     index: "Enterprise_Name",
                     width: 80
                 },
                  {
                      name: "CCEnterprise_Name",
                      index: "CCEnterprise_Name",
                      width: 80
                  },
                {
                    name: "pay_time",
                    index: "pay_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                },
                {
                    name: "create_time",
                    index: "create_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                }],
                caption: "支付记录",
                footerrow: true,
                userDataOnFooter: true
            });

            $("#grid").adaptGridSizeInWindow();
        });

        $('#search_pay_status').change(function () {
            var val = $(this).val();

            if (val == '3') {
                $('label[for="search_paytime_begin"]').html('支付时间');
            } else {
                $('label[for="search_paytime_begin"]').html('创建时间');
            }
        });

        $('#search-form').submit(function () {
            var data = $(this).serializeArray();
            var postData = {};
            $.each(data, function (i, n) {
                postData[n.name] = n.value;
            });

            $("#grid").jqGrid('setGridParam', {
                postData: postData, //发送数据
                page: 1
            }).trigger("reloadGrid"); //重新载入


            $('#btn-export').data('export-query', postData);

            return false;
        });


        $('#btn-export').click(function () {

            var postData = $('#btn-export').data('export-query');
            if (!postData) {
                toastr.error('请先执行查询');
                return;
            }
            $.postJsonAjax('/Admin/DownloadOrders', postData, function (rsp) {
                if (rsp.Code == 1) {
                    var el = $('<a id="download-file" style="display:none"></a>').appendTo(document.body);
                    el[0].href = rsp.Message;
                    el[0].click();
                    el.remove();
                } else {
                    toastr.error(rsp.Message);
                }
            });
        });

        var formatPayStatus = function (cellvalue, options, row) {
            if (cellvalue == 1) {
                //return '<span class="label label-default unpay" title="点击更新支付状态" data-order="' + row["order_id"] + '">未支付?</span>';
                return '<span class="label label-default">未支付</span>';
            }
            if (cellvalue == 2 || cellvalue == 3) {
                return '<span class="label label-success">已支付</span>';
            }
            if (cellvalue == 4) {
                return '<span class="label label-success">已退款</span>';
            }
            return '';
        }
        var formatPayType = function (cellvalue, options, row) {
            if (cellvalue == 6) {
                return '微信支付';
            }
            if (cellvalue == 7) {
                return '支付宝';
            }
            if (cellvalue == 8) {
                return '微信（通联）';
            }
            if (cellvalue == 9) {
                return '现金';
            }
            if (cellvalue == 5) {
                return '无感支付';
            }
            return '';
        }
        $('#btn-del').click(function () {
            var rowId = $("#grid").jqGrid("getGridParam", "selrow");
            var rowData = $("#grid").jqGrid('getRowData', rowId);
            if (!rowData) return;
            if (rowData.pay_status.indexOf("未支付") != -1) {
                toastr.error('该订单未支付');
            } else {
                $('#form-modal form').bindData(rowData);
                $('#form-modal').toggleModal(true, ' 编 辑 ');
            }
        });
        $.validator.setDefaults({
            submitHandler: function (form) {
                $(form).ajaxSubmit(function (d) {
                    $("#grid").trigger("reloadGrid");
                    $('#form-modal').toggleModal(false);
                    toastr.info('数据提交成功');
                });
            }
        });

        $('#form-modal form').validate()

        $('#btn-save').click(function (form) {
            $('#form-modal form').submit();
        });

    </script>

    <script>
        var start = {
            elem: '#search_paytime_begin',
            format: 'YYYY-MM-DD',
            max: laydate.now(), //最大日期
            istime: false,
            istoday: true,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };
        var end = {
            elem: '#search_paytime_end',
            format: 'YYYY-MM-DD',
            max: laydate.now(),
            istime: false,
            istoday: true,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(start);
        laydate(end);
    </script>

}
